package com.ss.android.download;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.CursorWrapper;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;

import com.bytedance.common.utility.Logger;
import com.ss.android.common.download.DownloadShortInfo;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class e {
    private static String[] a;
    private static e f;

    static {
        a = new String[]{"_id", "_data AS local_filename", "mediaprovider_uri", "destination", "title", "description", "uri", "status", "hint", "mimetype AS media_type", "total_bytes AS total_size", "lastmod AS last_modified_timestamp", "current_bytes AS bytes_so_far", "allow_write", "etag", "\'placeholder\' AS local_uri", "\'placeholder\' AS reason"};
    }

    private Context b;
    private k c;
    private String d;
    private Uri e;

    private e(Context arg2, String arg3) {

        this.e = com.ss.android.download.a.a.a;
        this.b = arg2.getApplicationContext();
        arg2.getApplicationContext().getContentResolver();
        this.c = k.a(this.b.getApplicationContext());
        this.d = arg3;
    }

    public static e a(Context arg3) {
        e v0_1;
        Class v1 = e.class;
        synchronized (v1) {
            if (f == null) {
                f = new e(arg3, arg3.getPackageName());
            }
            v0_1 = f;
        }
        return v0_1;
    }

    public static void a(Context arg6, int arg7, long arg8, String arg10) {
        long v4 = 0;
        if (arg6 != null && arg8 >= v4) {
            try {
                if (Logger.debug()) {
                    Logger.d("AppAdViewHolder", "mId = " + arg8 + " mStatus = " + arg7);
                }

                switch (arg7) {
                    case 1:
                    case 2: {
                        if (arg8 < v4) {
                            return;
                        }
                        a(arg6).d(new long[]{arg8});
                        return;
                    }
                    case 4: {
                        if (arg8 < v4) {
                            return;
                        }
                        a(arg6).e(new long[]{arg8});
                        return;
                    }
                    case 8: {
                        if (arg8 < v4) {
                            return;
                        }

                        android.support.design.a.a(arg6, arg8, arg10);
                        return;
                    }
                    case 16: {
                        if (arg8 < v4) {
                            return;
                        }

                        a(arg6).a(1, new long[]{arg8});
                        return;
                    }
                }
                return;
            } catch (Exception v0) {
                v0.printStackTrace();
            }
        }
    }

    public static void a(Context arg2, int arg3, long arg4) {
        a(arg2, arg3, arg4, null);
    }

    public static int a(int arg1) {
        int v0;
        switch (arg1) {
            case 190: {
                v0 = 1;
                break;
            }
            case 192: {
                v0 = 2;
                break;
            }
            case 193:
            case 194:
            case 195:
            case 196: {
                v0 = 4;
                break;
            }
            case 200:
            case 201: {
                v0 = 8;
                break;
            }
            default: {
                v0 = 16;
                break;
            }
        }

        return v0;
    }

    public static Long a() {
        return Long.valueOf(2147483648L);
    }

    static String a(String[] arg3) {
        StringBuilder v1 = new StringBuilder();
        v1.append("(");
        int v0;
        for (v0 = 0; v0 < arg3.length; ++v0) {
            if (v0 > 0) {
                v1.append("OR ");
            }

            v1.append("uri");
            v1.append(" = ? ");
        }

        v1.append(")");
        return v1.toString();
    }

    static boolean b(String arg1) {
        boolean v0 = TextUtils.isEmpty(((CharSequence) arg1)) ? false : new File(arg1).exists();
        return v0;
    }

    public static Long b() {
        return Long.valueOf(1073741824);
    }

    static String b(long[] arg3) {
        StringBuilder v1 = new StringBuilder();
        v1.append("(");
        int v0;
        for (v0 = 0; v0 < arg3.length; ++v0) {
            if (v0 > 0) {
                v1.append("OR ");
            }

            v1.append("_id");
            v1.append(" = ? ");
        }

        v1.append(")");
        return v1.toString();
    }

    static String[] b(String[] arg3) {
        String[] v1 = new String[arg3.length];
        int v0;
        for (v0 = 0; v0 < arg3.length; ++v0) {
            v1[v0] = arg3[v0];
        }

        return v1;
    }

    static String[] c(long[] arg4) {
        String[] v1 = new String[arg4.length];
        int v0;
        for (v0 = 0; v0 < arg4.length; ++v0) {
            v1[v0] = Long.toString(arg4[v0]);
        }

        return v1;
    }

    public final Cursor a(b arg4) {
        Cursor v0;
        Cursor v1 = arg4.a(this.c, a, this.e);
        if (v1 == null) {
            v0 = null;
        } else {
            v0 = new a(v1, this.e);
        }

        return v0;
    }

    public final long a(e$c arg7) {
        long v2 = -1;
        try {
            long v0_1 = this.b(arg7);
            if (v0_1 != v2) {
                return v0_1;
            }

            String v0_2 = this.d;
            ContentValues v1 = new ContentValues();
            if (!com.ss.android.download.e.e$c.j && arg7.a == null) {
                throw new AssertionError();
            }

            v1.put("uri", arg7.a.toString());
            v1.put("notificationpackage", v0_2);
            if (arg7.b != null) {
                v1.put("destination", Integer.valueOf(1));
                v1.put("hint", arg7.b.toString());
            } else {
                v1.put("destination", Integer.valueOf(0));
            }

            v1.put("scanned", Integer.valueOf(2));
            if (!arg7.c.isEmpty()) {
                arg7.a(v1);
            }

            com.ss.android.download.e.e$c.a(v1, "title", arg7.d);
            com.ss.android.download.e.e$c.a(v1, "description", null);
            com.ss.android.download.e.e$c.a(v1, "mimetype", arg7.e);
            v1.put("visibility", Integer.valueOf(arg7.i));
            v1.put("allowed_network_types", Integer.valueOf(arg7.f));
            v1.put("allow_roaming", Boolean.valueOf(arg7.g));
            v1.put("is_visible_in_downloads_ui", Boolean.valueOf(arg7.h));
            return Long.parseLong(this.c.a(com.ss.android.download.a.a.a, v1).getLastPathSegment());
        } catch (Exception v0) {
            return v2;
        }
    }

    public final int a(long[] arg7) {
        int v0_2;
        try {
            ContentValues v0_1 = new ContentValues();
            v0_1.put("deleted", Integer.valueOf(1));
            v0_2 = this.c.a(ContentUris.withAppendedId(this.e, arg7[0]), v0_1, null, null);
        } catch (Exception v0) {
            v0_2 = -1;
        }

        return v0_2;
    }

    public final DownloadShortInfo a(String arg7) {
        DownloadShortInfo v1_3;
        Cursor v2 = null;
        DownloadShortInfo v0 = null;
        if (!TextUtils.isEmpty(arg7)) {
            try {
                b v1_2 = new b();
                v1_2.b = new String[]{arg7};
                v2 = this.a(v1_2);
                if (v2 == null) {
                    return null;
                }
                if (v2.getCount() > 0) {
                    v2.moveToFirst();
                    v1_3 = new DownloadShortInfo();
                    v1_3.id = v2.getLong(v2.getColumnIndexOrThrow("_id"));
                    v1_3.b = v2.getInt(v2.getColumnIndexOrThrow("status"));
                    v1_3.c = v2.getLong(v2.getColumnIndexOrThrow("total_size"));
                    v1_3.d = v2.getLong(v2.getColumnIndexOrThrow("bytes_so_far"));
                    v1_3.fileName = v2.getString(v2.getColumnIndexOrThrow("local_filename"));
                    return v1_3;
                }
            } catch (Throwable v0_1) {
                v0_1.printStackTrace();
            } finally {
                if (v2 == null) {
                    return v0;
                }
                try {
                    v2.close();
                } catch (Exception v1_1) {
                }
            }
        }

        return v0;
    }

    public final void a(int arg7, long[] arg8) {
        b v0 = new b();
        v0.a = arg8;
        Cursor v1 = this.a(v0);
        try {
            v1.moveToFirst();
            while (!v1.isAfterLast()) {
                int v0_3 = v1.getInt(v1.getColumnIndex("status"));
                if (v0_3 != 8 && v0_3 != 16) {
                    throw new IllegalArgumentException("Cannot restart incomplete download: " + v1.getLong(v1.getColumnIndex("_id")));
                }

                v1.moveToNext();
            }

            ContentValues v0_4 = new ContentValues();
            v0_4.put("current_bytes", Integer.valueOf(0));
            v0_4.put("total_bytes", Integer.valueOf(-1));
            v0_4.putNull("_data");
            v0_4.put("status", Integer.valueOf(190));
            v0_4.put("numfailed", Integer.valueOf(0));
            v0_4.put("visibility", Integer.valueOf(arg7));
            v0_4.put("control", Integer.valueOf(0));
            this.c.a(this.e, v0_4, b(arg8), c(arg8));
        } catch (Exception v0_1) {
            v0_1.printStackTrace();
        } finally {
            if (v1 != null) {
                try {
                    v1.close();
                } catch (Exception v0_1) {
                }
            }
        }
    }

    final void a(long arg8) {
        Uri v1 = ContentUris.withAppendedId(com.ss.android.download.a.a.a, arg8);
        Cursor v2 = this.c.a(v1, null, null, null, null);
        try {
            if (!v2.moveToFirst()) {
                Log.w("DownloadManager", "Missing details for download " + arg8);
                return;
            }

            int v0_2 = v2.getInt(v2.getColumnIndexOrThrow("status"));
            int v3 = v2.getInt(v2.getColumnIndexOrThrow("visibility"));
            String v4 = v2.getString(v2.getColumnIndexOrThrow("_data"));
            if (com.ss.android.download.a.a.a(v0_2) && (v3 == 0 || v3 == 1) && b(v4)) {
                ContentValues v0_3 = new ContentValues();
                v0_3.put("visibility", Integer.valueOf(1));
                v0_3.put("status", Integer.valueOf(201));
                this.c.a(v1, v0_3, null, null);
            } else {
                if (a(v0_2) == 4) {
                    this.e(new long[]{arg8});
                    return;
                }
                this.a(v3, new long[]{arg8});
            }
        } catch (Exception v0) {
            v0.printStackTrace();
        } finally {
            if (v2 != null) {
                try {
                    v2.close();
                } catch (Exception v1_1) {
                }
            }
        }
    }

    private long b(e$c arg8) {
        String v3;
        long v4;
        Cursor v6 = null;
        Cursor v1 = null;
        try {
            b v0_2 = new b();
            v0_2.b = new String[]{e$c.a(arg8).toString()};
            v6 = this.a(v0_2);
            if (v6 == null) {
                return -1;
            }
            if (v6.getCount() > 0) {
                v6.moveToFirst();
                v4 = v6.getLong(v6.getColumnIndexOrThrow("_id"));
                v3 = v6.getString(v6.getColumnIndexOrThrow("etag"));
                if (TextUtils.isEmpty(v3)) {
                    this.a(v4);
                    return v4;
                } else if (!android.support.design.a.o().a(this.b)) {
                    this.a(v4);
                    return v4;
                } else {
                    com.bytedance.common.utility.b.a.a(new com.ss.android.download.f(this, arg8, v3, v4), new Void[0]);
                    return v4;
                }
            }
            return -1;
        } catch (Exception e) {

        } finally {
            if (v6 != null) {
                try {
                    v6.close();
                } catch (Exception v0_1) {
                }
            }
        }
        return -1;
    }

    private void d(long[] arg7) {
        b v0 = new b();
        v0.a = arg7;
        Cursor v1 = this.a(v0);
        try {
            v1.moveToFirst();
            while (!v1.isAfterLast()) {
                int v0_3 = v1.getInt(v1.getColumnIndex("status"));
                if (v0_3 != 2 && v0_3 != 1) {
                    throw new IllegalArgumentException("Can only pause a running download: " + v1.getLong(v1.getColumnIndex("_id")));
                }
                v1.moveToNext();
            }

            ContentValues v0_4 = new ContentValues();
            v0_4.put("control", Integer.valueOf(1));
            v0_4.put("no_integrity", Integer.valueOf(1));
            this.c.a(this.e, v0_4, b(arg7), c(arg7));
            if (v1 == null) {
                return;
            }
        } catch (Exception v0_1) {
            v0_1.printStackTrace();
        } finally {
            if (v1 != null) {
                try {
                    v1.close();
                } catch (Exception v1_1) {
                }
            }
        }
    }

    private void e(long[] arg7) {
        b v0 = new b();
        v0.a = arg7;
        Cursor v1 = this.a(v0);
        try {
            v1.moveToFirst();
            while (!v1.isAfterLast()) {
                if (v1.getInt(v1.getColumnIndex("status")) != 4) {
                    throw new IllegalArgumentException("Cann only resume a paused download: " + v1.getLong(v1.getColumnIndex("_id")));
                }
                v1.moveToNext();
            }
            ContentValues v0_3 = new ContentValues();
            v0_3.put("status", Integer.valueOf(190));
            v0_3.put("control", Integer.valueOf(0));
            this.c.a(this.e, v0_3, b(arg7), c(arg7));
            return;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (v1 != null) {
                try {
                    v1.close();
                } catch (Exception e) {

                }
            }
        }
    }

    public static final class b {
        public long[] a;
        String[] b;
        Integer c;
        private String d;
        private int e;

        public b() {
            this.a = null;
            this.b = null;
            this.c = null;
            this.d = "lastmod";
            this.e = 2;
        }

        private static String a(String arg2, int arg3) {
            return "status" + arg2 + "\'" + arg3 + "\'";
        }

        private static String a(String arg4, Iterable arg5) {
            StringBuilder v2 = new StringBuilder();
            Iterator v3 = arg5.iterator();
            int v1;
            for (v1 = 1; v3.hasNext(); v1 = 0) {
                Object v0 = v3.next();
                if (v1 == 0) {
                    v2.append(arg4);
                }

                v2.append(((String) v0));
            }

            return v2.toString();
        }

        final Cursor a(k arg8, String[] arg9, Uri arg10) {
            Cursor v6_1 = null;
            Object[] v4;
            String[] v6 = null;
            ArrayList v0 = new ArrayList();
            ArrayList v1 = new ArrayList();
            if (this.a != null) {
                ((List) v0).add(b(this.a));
                ((List) v1).addAll(Arrays.asList(c(this.a)));
            }

            if (this.b != null) {
                ((List) v0).add(com.ss.android.download.e.a(this.b));
                ((List) v1).addAll(Arrays.asList(com.ss.android.download.e.b(this.b)));
            }

            if (this.c != null) {
                ArrayList v2 = new ArrayList();
                if ((this.c.intValue() & 1) != 0) {
                    ((List) v2).add(a("=", 190));
                }

                if ((this.c.intValue() & 2) != 0) {
                    ((List) v2).add(a("=", 192));
                }

                if ((this.c.intValue() & 4) != 0) {
                    ((List) v2).add(a("=", 193));
                    ((List) v2).add(a("=", 194));
                    ((List) v2).add(a("=", 195));
                    ((List) v2).add(a("=", 196));
                }

                if ((this.c.intValue() & 8) != 0) {
                    ((List) v2).add(a("=", 200));
                }

                if ((this.c.intValue() & 16) != 0) {
                    ((List) v2).add("(" + a(">=", 400) + " AND " + a("<", 600) + ")");
                }

                ((List) v0).add(a(" OR ", v2));
            }

            ((List) v0).add("deleted != \'1\'");
            String v3 = a(" AND ", v0);
            try {
                v4 = ((List) v1).toArray(new String[((List) v1).size()]);
            } catch (ArrayStoreException v0_1) {
                v0_1.printStackTrace();
                v4 = v6;
            }

            String v0_2 = this.e == 1 ? "ASC" : "DESC";
            String v5 = this.d + " " + v0_2;
            k v0_3 = arg8;
            Uri v1_1 = arg10;
            String[] v2_1 = arg9;
            try {
                v6_1 = v0_3.a(v1_1, v2_1, v3, ((String[]) v4), v5);
            } catch (Exception v0_4) {
            }

            return v6_1;
        }
    }

    public static final class e$c {
        static final boolean j;

        static {
            boolean v0 = !e.class.desiredAssertionStatus() ? true : false;
            j = v0;
        }

        Uri a;
        Uri b;
        List c;
        CharSequence d;
        String e;
        int f;
        boolean g;
        boolean h;
        int i;

        public e$c(Uri arg4) {
            this.c = new ArrayList();
            this.f = -1;
            this.g = true;
            this.h = true;
            this.i = 0;
            if (arg4 == null) {
                throw new NullPointerException();
            }

            String v0 = arg4.getScheme();
            if (v0 != null && ((v0.equals("http")) || (v0.equals("https")))) {
                this.a = arg4;
                return;
            }

            throw new IllegalArgumentException("Can only download HTTP/HTTPS URIs: " + arg4);
        }

        static Uri a(e$c arg1) {
            return arg1.a;
        }

        static void a(ContentValues arg1, String arg2, Object arg3) {
            if (arg3 != null) {
                arg1.put(arg2, arg3.toString());
            }
        }

        static int b(e$c arg1) {
            return arg1.i;
        }

        final void a(ContentValues arg6) {
            Iterator v3 = this.c.iterator();
            int v2;
            for (v2 = 0; v3.hasNext(); ++v2) {
                Object v0 = v3.next();
                arg6.put("http_header_" + v2, ((Pair) v0).first + ": " + ((Pair) v0).second);
            }
        }
    }

    final class a extends CursorWrapper {
        private Uri a;

        public a(Cursor arg1, Uri arg2) {
            super(arg1);
            this.a = arg2;
        }

        public final int getInt(int arg3) {
            return ((int) this.getLong(arg3));
        }

        public final long getLong(int arg3) {
            long v0_1;
            if (this.getColumnName(arg3).equals("reason")) {
                int v0 = super.getInt(this.getColumnIndex("status"));
                switch (a(v0)) {
                    case 4: {
                        switch (v0) {
                            case 194: {
                                return 1;
                            }
                            case 195: {
                                return 2;
                            }
                            case 196: {
                                return 3;
                            }
                        }
                        return 4;
                    }
                    case 16: {
                        if (400 <= v0 && v0 < 488 || 500 <= v0 && v0 < 600) {
                            return ((long) v0);
                        }
                        switch (v0) {
                            case 198: {
                                return 1006;
                            }
                            case 199: {
                                return 1007;
                            }
                            case 488: {
                                return 1009;
                            }
                            case 489: {
                                return 1008;
                            }
                            case 492: {
                                return 1001;
                            }
                            case 493:
                            case 494: {
                                return 1002;
                            }
                            case 495: {
                                return 1004;
                            }
                            case 497: {
                                return 1005;
                            }
                        }

                        return 1000;
                    }
                }

                v0_1 = 0;
                return v0_1;
            } else {
                if (this.getColumnName(arg3).equals("status")) {
                    return ((long) a(super.getInt(this.getColumnIndex("status"))));
                }

                v0_1 = super.getLong(arg3);
            }

            return v0_1;
        }

        public final String getString(int arg5) {
            String v0_1;
            if (this.getColumnName(arg5).equals("local_uri")) {
                long v0 = this.getLong(this.getColumnIndex("destination"));
                if (v0 != 1 && v0 != 0) {
                    v0_1 = ContentUris.withAppendedId(this.a, this.getLong(this.getColumnIndex("_id"))).toString();
                    return v0_1;
                }

                v0_1 = this.getString(this.getColumnIndex("local_filename"));
                if (v0_1 == null) {
                    return null;
                }

                v0_1 = Uri.fromFile(new File(v0_1)).toString();
            } else {
                v0_1 = super.getString(arg5);
            }

            return v0_1;
        }
    }
}

